package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.domain.Customer;
import cn.congzhou.crm.domain.Employee;
import cn.congzhou.crm.mapper.CustomerMapper;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.ICustomerService;
import cn.congzhou.crm.utils.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

//   更新客户状态
    @Override
    public void changeCustomerStatus(Long cusId, Integer status) {
        if(cusId!=null){
            customerMapper.changeCustomerStatus(cusId, status);
        }
    }


    @Override
    public Customer selectByPrimaryKey(Long id) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        return customer;
    }

    @Override
    public List<Customer> selectAll() {
        List<Customer> customer = customerMapper.selectAll();
        return customer;
    }

    @Override
    public void saveOrUpdate(Customer customer) {
        if(customer.getId()!=null){
            customerMapper.updateByPrimaryKey(customer);
        }else {
//          保存时添加默认数据
            Employee emp =UserContext.getCurrentUser();
            customer.setInputTime(new Date());
            customer.setSeller(emp);
            customer.setInputUser(emp);
            customer.setStatus(0);
            customerMapper.insert(customer);
        }
    }

    @Override
    public PageInfo<Customer> selectByPage(QueryObject qo) {
//      调用这个类的静态方法处理分页数据
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Customer> list = customerMapper.selectByPage(qo);
//      创建页面信息对象封装起来
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void insertOneCustomer(Customer customer) {
        if (customer == null){
            return;
        }
        // 保存时添加默认数据
        Employee emp =UserContext.getCurrentUser();
        if (customer.getInputTime() == null){
            customer.setInputTime(new Date());
        }
        customer.setInputUser(emp);
        if(customer.getStatus() == null){
            customer.setStatus(0);
        }
        customerMapper.insert(customer);
    }

    @Override
    public List<Customer> exportCustomer(QueryObject qo) {
        List<Customer> list = customerMapper.selectByPage(qo);
        return list;
    }
}
